var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var page = 1;
var wxAuthFail = true; 

var inviteuid = '';

Page({  
  data: {  
    uid: 0,
    tc: 0,
    token: '',
    userinfo:{},


    colorSet: '',

    hasShare: false,

    bottom_nav_data: [],
    isIphone_x: false,
    nav_bgColor: '#ffffff',

    userinfo: {},
    menu:false, //是否需要绑定

    refereesArray:[], //管理者信息集合
    coachArray:[], //教练信息集合
    studentArray:[], //学员信息集合

    coachStudent:[], //教练名的学员
    studentInfo:[],  //学习获取自己信息
    refereesInfo: [], //管理员页面获取的信息
  },  
   
  onLoad: function (data) {  
    var that = this;
    var tc = app.get_tc();//简体繁体转换
    that.setData({
      tc: tc
    })
    var titlename = tc == 1 ? '东和菜单' : '东和菜单';
    wx.setNavigationBarTitle({
      title: titlename,
    }); 
 
    //初始化底部导航
    var b_nav_data = app.getBottomNavData('east_menu/east_menu', function (data) {
      that.setData({
        bottom_nav_data: data,
        nav_bgColor: data[0].bgcolor
      });
    });
    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color,
      })
      // that.resetNavColor();
    });

    wx.getSystemInfo({
      success: function (res) {
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          isIphone_x: isIphone_x,
        });
      }
    });

     //当前登录用户信息
    app.LoginUserInfo(function (res) {
      var authFail = res == -1;//用户拒绝授权
      if (!authFail) {
        //更新数据
        that.setData({
          uid: res.uid,
          token: res.token,
          openid: res.wxsmall_openid
        });


      }

    });

    that.getCTUser();
  },  


  onShow: function () {

  },

  //判断是够已经绑定了东和信息的用户
  getCTUser:function(){
    var that = this;
    util.api({
        data: {
          "publickey": util.publiccode,
          "ac": "getCTUser",
          "uid": that.data.uid,
          "orgid":util.orgid,
        },
        
        success: function (responseData) {
          
          console.log("判断结果==", responseData.data);
          if(responseData.data.res != false){
              var key = responseData.data.key;
              var result = responseData.data.result;
              if(key == "1"){
                that.setData({
                  refereesArray:result,
                  menu:false
                })

                that.refereesGetInfo(); 

              }else if(key == "2"){
                that.setData({
                  coachArray:result,
                  menu:false
                })

                that.coachGetStudent(result['id']);
              }else if(key == "3"){
                that.setData({
                  studentArray:result,
                  menu:false
                })

                that.studentGetinfo(result['id']);
              }
          }else{
            that.setData({
              menu:true
            })
          }
        } 
      });
  },


  //管理者页面--获取所有信息
  refereesGetInfo:function(){
    var that = this;
    util.api({
        data: {
          "publickey": util.publiccode,
          "ac": "refereesGetInfo",
          "uid": that.data.uid,

        },
        
        success: function (responseData) {
          console.log("教练信息==", responseData.data);
          that.setData({
            refereesInfo:responseData.data
          })
        } 
      });
  },

  //教练页面--获取教练名下的学生
  coachGetStudent:function(coachId){
    var that = this;
    util.api({
        data: {
          "publickey": util.publiccode,
          "ac": "coachGetStudent",
          "uid": that.data.uid,
          "coachId":coachId,
        },
        
        success: function (responseData) {
          console.log("学生信息==", responseData.data);
          that.setData({
            coachStudent:responseData.data
          })
        } 
      });
  },


   //学员页面--获取自己的信息
  studentGetinfo:function(studentId){
    var that = this;
    util.api({
        data: {
          "publickey": util.publiccode,
          "ac": "studentGetinfo",
          "uid": that.data.uid,
          "studentId":studentId,
        },
        
        success: function (responseData) {
          console.log("学生信息==", responseData.data);
          that.setData({
            studentInfo:responseData.data
          })
        } 
      });
  },


  //绑定手机号提交事件
  formSubmit:function(e){
    app.saveFormId(e.detail.formId);
    var mobile = e.detail.value.mobile;
 
     
     var that = this;

     if(mobile == ''){
        var contentname = this.data.tc == 1 ? '手機號不能為空' : '手机号不能為空';
        wx.showModal({
          title: '提示',
          content: contentname,
          showCancel: false,
        })
        return;
     }
     
    util.api({
        data: {
          "publickey": util.publiccode,
          "ac": "CTRegist",
          "uid": that.data.uid,
          "orgid":util.orgid,
          "phone":mobile,
    
        },
        
        success: function (responseData) {
          
          console.log("绑定", responseData.data);
          var key = responseData.data.key;
          var result = responseData.data.result;
          if(key == "1"){
            that.setData({
              refereesArray:result,
              menu:false
            })
          }else if(key == "2"){
            that.setData({
              coachArray:result,
              menu:false
            })
            that.coachGetStudent(result['id']);
          }else if(key == "3"){
            that.setData({
              studentArray:result,
              menu:false
            })
            that.studentGetinfo(result['id']);
          }else{
            that.setData({
              menu:true
            })
          }
        
        } 
      });
  },

  //跳转页面
  goAtherPage: function (e) {
    var url = e.currentTarget.dataset.link;
    if (url) {
      wx.navigateTo({ url: url });
    }
  },

  //保存formId
  submitFormId: function (e) {
    app.saveFormId(e.detail.formId);

    var path = e.detail.value.nav_path;
    if (path && path.indexOf('navto:') == 0) {
      var navPath = path.slice(6);
      wx.navigateTo({
        url: navPath,
      })
    } else {
      wx.redirectTo({ url: path });
    }
  },
})
